#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
生成占位图片脚本
使用 PIL/Pillow 生成简单的占位图片
需要安装: pip install pillow
"""

from PIL import Image, ImageDraw, ImageFont
import os

# 创建图片目录
images_dir = 'images/words'
os.makedirs(images_dir, exist_ok=True)

# 单词列表（英文单词 -> 文件名）
words_map = {
    # 动物
    'cat': 'cat.png',
    'dog': 'dog.png',
    'bird': 'bird.png',
    'fish': 'fish.png',
    'rabbit': 'rabbit.png',
    'tiger': 'tiger.png',
    'lion': 'lion.png',
    'elephant': 'elephant.png',
    # 水果
    'apple': 'apple.png',
    'banana': 'banana.png',
    'orange': 'orange.png',
    'grape': 'grape.png',
    'strawberry': 'strawberry.png',
    'watermelon': 'watermelon.png',
    'pear': 'pear.png',
    'peach': 'peach.png',
    # 颜色
    'red': 'red.png',
    'blue': 'blue.png',
    'green': 'green.png',
    'yellow': 'yellow.png',
    'orange color': 'orange-color.png',
    'purple': 'purple.png',
    'pink': 'pink.png',
    'black': 'black.png',
    # 数字
    'one': 'one.png',
    'two': 'two.png',
    'three': 'three.png',
    'four': 'four.png',
    'five': 'five.png',
    'six': 'six.png',
    'seven': 'seven.png',
    'eight': 'eight.png',
    'nine': 'nine.png',
    'ten': 'ten.png',
}

# 颜色映射（用于颜色类单词）
color_map = {
    'red': (255, 0, 0),
    'blue': (0, 0, 255),
    'green': (0, 128, 0),
    'yellow': (255, 255, 0),
    'orange color': (255, 165, 0),
    'purple': (128, 0, 128),
    'pink': (255, 192, 203),
    'black': (0, 0, 0),
}

def generate_placeholder_image(word, filename):
    """生成占位图片"""
    try:
        # 图片尺寸
        width, height = 400, 400
        
        # 创建图片
        img = Image.new('RGB', (width, height), color='white')
        draw = ImageDraw.Draw(img)
        
        # 如果是颜色类单词，使用对应颜色作为背景
        if word.lower() in color_map:
            bg_color = color_map[word.lower()]
            img = Image.new('RGB', (width, height), color=bg_color)
            draw = ImageDraw.Draw(img)
            text_color = (255, 255, 255) if word.lower() in ['black', 'blue', 'purple'] else (0, 0, 0)
        else:
            # 其他单词使用渐变色背景
            bg_color = (74, 144, 226)  # 蓝色
            img = Image.new('RGB', (width, height), color=bg_color)
            draw = ImageDraw.Draw(img)
            text_color = (255, 255, 255)
        
        # 绘制圆角矩形背景
        margin = 20
        draw.rounded_rectangle(
            [margin, margin, width - margin, height - margin],
            radius=30,
            fill=(255, 255, 255, 200) if word.lower() not in color_map else None,
            outline=text_color,
            width=3
        )
        
        # 添加文字
        try:
            # 尝试使用系统字体
            font_size = 60
            font = ImageFont.truetype("/System/Library/Fonts/Helvetica.ttc", font_size)
        except:
            # 如果失败，使用默认字体
            font = ImageFont.load_default()
        
        # 计算文字位置（居中）
        text = word.upper()
        bbox = draw.textbbox((0, 0), text, font=font)
        text_width = bbox[2] - bbox[0]
        text_height = bbox[3] - bbox[1]
        
        x = (width - text_width) // 2
        y = (height - text_height) // 2 - 20
        
        # 绘制文字阴影
        draw.text((x + 2, y + 2), text, font=font, fill=(0, 0, 0, 128))
        # 绘制文字
        draw.text((x, y), text, font=font, fill=text_color)
        
        # 保存图片
        filepath = os.path.join(images_dir, filename)
        img.save(filepath, 'PNG')
        print(f"✓ 生成: {filename}")
        return True
    except Exception as e:
        print(f"✗ 生成失败: {filename} - {e}")
        return False

print("开始生成占位图片...\n")

success_count = 0
fail_count = 0

for word, filename in words_map.items():
    if os.path.exists(os.path.join(images_dir, filename)):
        print(f"○ 已存在: {filename}")
        success_count += 1
        continue
    
    if generate_placeholder_image(word, filename):
        success_count += 1
    else:
        fail_count += 1

print(f"\n生成完成！")
print(f"成功: {success_count} 个")
print(f"失败: {fail_count} 个")
print(f"文件保存在 {images_dir}/ 目录")
print("\n提示：这些是占位图片，建议后续替换为真实图片")

